<?php
class LoginView {
	// Define and assign private variables
	private $username = "username";
	private $password = "password";
	private $loginButton = "login";
	private $logoutButton = "logout";
	private $rememberMe = "rememberMe";
	private $loginFail = "Fel lösenord eller användarnamn";
	private $passwordValidation = "Felaktigt lösenord. Lösenordet måste vara 6-12 tecken långt och kan bestå av bokstäver, siffror och något av följande tecken: !?@#";
	private $usernameValidation = "Felaktigt användarnamn. Användarnamnet måste vara 6-12 tecken långt och kan bestå av bokstäver, siffror, - och _";
	public $errormessage;

	public function createContainer() {
		return "<div class='box'>";
	}
	public function createLoginBox() {
		// Returns xhtml string (a loginbox)
		$loginbox = "<form action='' method='get' class='loginform'>
					<fieldset>
						<label for='$this->username'>Namn:</label> <input type='text' id='$this->username' name='$this->username' /> <br /> 
						<label for='$this->password'>Lösenord:</label> <input type='password'  id='$this->password' name='$this->password' /> <br />
						<input type='submit' name='$this->loginButton' value='Logga in' /> <label for='$this->rememberMe'>Kom ihåg mig</label> <input type='checkbox' checked='checked' name='$this->rememberMe' id='$this->rememberMe'/>
					</fieldset>
					</form>";
		$status = "<p class='status'>Du är utloggad</p>";
		return $status . $loginbox;
	}
	public function showFeedback($action) {
		// Returns passed value enclosed by paragraph tags
		$msg = "<p class='error'>$action</p>";
		return $msg;
	}
	public function createLoggedInBox() {
		// Returns xhtml string (a logout button)
		$logoutbox = "<form action='' method='get' class='loginform'> 
				<input type='submit' name='$this->logoutButton' value='Logga ut' />
				</form>";
		$status = "<p class='status'>Du är inloggad</p>";
		return $status . $logoutbox;
	}
	public function getUserName() {
		$validate = new validate();
		// Get and return username, if name has been input...
		if (isset($_GET[$this -> username]) == true) {
			if(!$validate->validateUsername($_GET[$this -> username])){
				// If username does not validate, return errormessage
				$this->errormessage = $this->usernameValidation;
			}
			return $_GET[$this -> username];
		}
		// ...or if there's a cookie saved
		else if (isset($_COOKIE[$this -> username])) {
			if(!$validate->validateUsername($_COOKIE[$this -> username])){
				// If username does not validate, return errormessage
				$this->errormessage = $this->usernameValidation;
			}
			return $_COOKIE[$this -> username];
		} else {
			return null;
		}
	}
	public function getPassword() {
		$validate = new validate();
		// Get and return password, if password has been input...
		if (isset($_GET[$this -> password]) == true) {
			if (!$validate -> validatePassword($_GET[$this -> password])) {
				// If password does not validate, return errormessage
				$this->errormessage = $this->passwordValidation;
			};
			return $_GET[$this -> password];
		}
		// ...or if there's a cookie saved
		else if (isset($_COOKIE[$this -> password])) {
			if (!$validate -> validatePassword($_COOKIE[$this -> password])) {
				// If password does not validate, return errormessage
				$this->errormessage = $this->passwordValidation;
			};
			return $_COOKIE[$this -> password];
		} else {
			return null;
		}
	}
	public function createCookie($name, $value) {
		// Set cookie
		setcookie($name, $value, time() + 3600);
		// For automatic testing
		$_COOKIE[$name] = $value;
	}
	public function destroyCookie($name) {
		// Destroy cookie
		setcookie($name, "", time() - 3600);
		unset($_COOKIE[$name]);
	}
	public function rememberMe() {
		// Returns true if user checked 'Remember me'- checkbox
		return isset($_GET[$this -> rememberMe]);
	}
	public function triedToLogin() {
		// Returns true if login button has been clicked
		if(isset($_GET[$this->loginButton]) == true) {
			if($this -> rememberMe()){
				$loginHandler = new LoginHandler();
				if($loginHandler->doLogin($this->getUserName(), $this->getPassword())){
					// If login was succesful and user checked 'Remember me', save username and password in cookies
					$this -> createCookie($this->username, $this->getUserName());
					$this -> createCookie($this->password, $this->getPassword());
				}
				else{$this->errormessage = $this->loginFail;}
			}
			return true;
		}
		// or if there's a cookie saved
		else if(isset($_COOKIE[$this->username]) && isset($_COOKIE[$this->password])){
			return true;
		}
		// else return false
		else {return false;}
	}
	public function triedToLogout(){
		// Returns true if logout button has been clicked, else returns false
		if(isset($_GET[$this->logoutButton]) == true) {
			// Destroy cookies
			$this->destroyCookie($this->username);	
			$this->destroyCookie($this->password);
			return true;
		}
		else {return false;}
	}

}
?>